﻿// =================================================================== 
// 项目说明
//====================================================================
// SunCity@Copy Right 2006-2008
// 文件： News.cs
// 项目名称：SunCity_MS
// 创建时间：2015/3/18
// 负责人：Chris
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using EMS20.Entity;
using EMS20.IDataAccessLayer;

namespace EMS20.DataAccessLayer
{
    /// <summary>
    /// 数据层实例化接口类 dbo.News.
    /// </summary>
    public partial class NewsDataAccessLayer : INewsDataAccessLayer
    {
		#region 构造函数
		/// <summary>
		/// ??????
		/// </summary>
		public NewsDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_NewsModel">News实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_NewsInsert(NewsEntity _NewsModel)
		{
			string sqlStr="CreateUpdateDelete_NewsEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@n_id",SqlDbType.Int),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@n_title",SqlDbType.VarChar),
			new SqlParameter("@n_author",SqlDbType.VarChar),
			new SqlParameter("@n_form",SqlDbType.VarChar),
			new SqlParameter("@n_content",SqlDbType.VarChar),
			new SqlParameter("@n_sort_id",SqlDbType.Int),
			new SqlParameter("@n_click",SqlDbType.Int),
			new SqlParameter("@n_is_lock",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime)
			};
			_param[0].Value=0;
			_param[1].Value=_NewsModel.n_id;
			_param[2].Value=_NewsModel.category_id;
			_param[3].Value=_NewsModel.user_id;
			_param[4].Value=_NewsModel.n_title;
			_param[5].Value=_NewsModel.n_author;
			_param[6].Value=_NewsModel.n_form;
			_param[7].Value=_NewsModel.n_content;
			_param[8].Value=_NewsModel.n_sort_id;
			_param[9].Value=_NewsModel.n_click;
			_param[10].Value=_NewsModel.n_is_lock;
			_param[11].Value=_NewsModel.add_time;
			res = (int)SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_NewsModel">News实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_NewsInsert(SqlTransaction sp,NewsEntity _NewsModel)
		{
			string sqlStr="CreateUpdateDelete_NewsEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@n_id",SqlDbType.Int),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@n_title",SqlDbType.VarChar),
			new SqlParameter("@n_author",SqlDbType.VarChar),
			new SqlParameter("@n_form",SqlDbType.VarChar),
			new SqlParameter("@n_content",SqlDbType.VarChar),
			new SqlParameter("@n_sort_id",SqlDbType.Int),
			new SqlParameter("@n_click",SqlDbType.Int),
			new SqlParameter("@n_is_lock",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime)
			};
			_param[0].Value=0;
			_param[1].Value=_NewsModel.n_id;
			_param[2].Value=_NewsModel.category_id;
			_param[3].Value=_NewsModel.user_id;
			_param[4].Value=_NewsModel.n_title;
			_param[5].Value=_NewsModel.n_author;
			_param[6].Value=_NewsModel.n_form;
			_param[7].Value=_NewsModel.n_content;
			_param[8].Value=_NewsModel.n_sort_id;
			_param[9].Value=_NewsModel.n_click;
			_param[10].Value=_NewsModel.n_is_lock;
			_param[11].Value=_NewsModel.add_time;
			res = (int)SqlHelper.ExecuteScalar(sp,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}


		/// <summary>
		/// 向数据表News更新一条记录。
		/// </summary>
		/// <param name="_NewsModel">_NewsModel</param>
		/// <returns>影响的行数</returns>
		public int Create_NewsUpdate(NewsEntity _NewsModel)
		{
            string sqlStr="CreateUpdateDelete_NewsEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@n_id",SqlDbType.Int),
				new SqlParameter("@category_id",SqlDbType.Int),
				new SqlParameter("@user_id",SqlDbType.Int),
				new SqlParameter("@n_title",SqlDbType.VarChar),
				new SqlParameter("@n_author",SqlDbType.VarChar),
				new SqlParameter("@n_form",SqlDbType.VarChar),
				new SqlParameter("@n_content",SqlDbType.VarChar),
				new SqlParameter("@n_sort_id",SqlDbType.Int),
				new SqlParameter("@n_click",SqlDbType.Int),
				new SqlParameter("@n_is_lock",SqlDbType.Int),
				new SqlParameter("@add_time",SqlDbType.DateTime)
				};
				_param[0].Value=1;
				_param[1].Value=_NewsModel.n_id;
				_param[2].Value=_NewsModel.category_id;
				_param[3].Value=_NewsModel.user_id;
				_param[4].Value=_NewsModel.n_title;
				_param[5].Value=_NewsModel.n_author;
				_param[6].Value=_NewsModel.n_form;
				_param[7].Value=_NewsModel.n_content;
				_param[8].Value=_NewsModel.n_sort_id;
				_param[9].Value=_NewsModel.n_click;
				_param[10].Value=_NewsModel.n_is_lock;
				_param[11].Value=_NewsModel.add_time;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表News更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_NewsModel">_NewsModel</param>
		/// <returns>影响的行数</returns>
		public int Create_NewsUpdate(SqlTransaction sp,NewsEntity _NewsModel)
		{
            string sqlStr="CreateUpdateDelete_NewsEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@n_id",SqlDbType.Int),
				new SqlParameter("@category_id",SqlDbType.Int),
				new SqlParameter("@user_id",SqlDbType.Int),
				new SqlParameter("@n_title",SqlDbType.VarChar),
				new SqlParameter("@n_author",SqlDbType.VarChar),
				new SqlParameter("@n_form",SqlDbType.VarChar),
				new SqlParameter("@n_content",SqlDbType.VarChar),
				new SqlParameter("@n_sort_id",SqlDbType.Int),
				new SqlParameter("@n_click",SqlDbType.Int),
				new SqlParameter("@n_is_lock",SqlDbType.Int),
				new SqlParameter("@add_time",SqlDbType.DateTime)
				};
				_param[0].Value=1;
				_param[1].Value=_NewsModel.n_id;
				_param[2].Value=_NewsModel.category_id;
				_param[3].Value=_NewsModel.user_id;
				_param[4].Value=_NewsModel.n_title;
				_param[5].Value=_NewsModel.n_author;
				_param[6].Value=_NewsModel.n_form;
				_param[7].Value=_NewsModel.n_content;
				_param[8].Value=_NewsModel.n_sort_id;
				_param[9].Value=_NewsModel.n_click;
				_param[10].Value=_NewsModel.n_is_lock;
				_param[11].Value=_NewsModel.add_time;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表News中的一条记录
		/// </summary>
	    /// <param name="n_id">n_id</param>
		/// <returns>影响的行数</returns>
		public int Create_NewsDelete(int n_id)
		{
        NewsEntity _NewsModel =new NewsEntity();
			string sqlStr="CreateUpdateDelete_NewsEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.VarChar),
			new SqlParameter("@n_id",SqlDbType.Int),			
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@n_title",SqlDbType.VarChar),
			new SqlParameter("@n_author",SqlDbType.VarChar),
			new SqlParameter("@n_form",SqlDbType.VarChar),
			new SqlParameter("@n_content",SqlDbType.VarChar),
			new SqlParameter("@n_sort_id",SqlDbType.Int),
			new SqlParameter("@n_click",SqlDbType.Int),
			new SqlParameter("@n_is_lock",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime)
			};
			_param[0].Value=2;
			_param[1].Value=n_id;
			_param[2].Value=_NewsModel.category_id;
			_param[3].Value=_NewsModel.user_id;
			_param[4].Value=_NewsModel.n_title;
			_param[5].Value=_NewsModel.n_author;
			_param[6].Value=_NewsModel.n_form;
			_param[7].Value=_NewsModel.n_content;
			_param[8].Value=_NewsModel.n_sort_id;
			_param[9].Value=_NewsModel.n_click;
			_param[10].Value=_NewsModel.n_is_lock;
			_param[11].Value=_NewsModel.add_time;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表News中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="n_id">n_id</param>
		/// <returns>影响的行数</returns>
		public int Create_NewsDelete(SqlTransaction sp,int n_id)
		{
         NewsEntity _NewsModel =new NewsEntity();
			string sqlStr="CreateUpdateDelete_NewsEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@n_id",SqlDbType.Int),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@user_id",SqlDbType.Int),
			new SqlParameter("@n_title",SqlDbType.VarChar),
			new SqlParameter("@n_author",SqlDbType.VarChar),
			new SqlParameter("@n_form",SqlDbType.VarChar),
			new SqlParameter("@n_content",SqlDbType.VarChar),
			new SqlParameter("@n_sort_id",SqlDbType.Int),
			new SqlParameter("@n_click",SqlDbType.Int),
			new SqlParameter("@n_is_lock",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime)
			};
			_param[0].Value=2;
			_param[1].Value=n_id;
			_param[2].Value=_NewsModel.category_id;
			_param[3].Value=_NewsModel.user_id;
			_param[4].Value=_NewsModel.n_title;
			_param[5].Value=_NewsModel.n_author;
			_param[6].Value=_NewsModel.n_form;
			_param[7].Value=_NewsModel.n_content;
			_param[8].Value=_NewsModel.n_sort_id;
			_param[9].Value=_NewsModel.n_click;
			_param[10].Value=_NewsModel.n_is_lock;
			_param[11].Value=_NewsModel.add_time;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  news 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>news 数据实体</returns>
		public NewsEntity Populate_NewsEntity_FromDr(DataRow row)
		{
			NewsEntity Obj = new NewsEntity();
			if(row!=null)
			{
				Obj.n_id = (( row["n_id"])==DBNull.Value)?0:Convert.ToInt32( row["n_id"]);
				Obj.category_id = (( row["category_id"])==DBNull.Value)?0:Convert.ToInt32( row["category_id"]);
				Obj.user_id = (( row["user_id"])==DBNull.Value)?0:Convert.ToInt32( row["user_id"]);
				Obj.n_title =  row["n_title"].ToString();
				Obj.n_author =  row["n_author"].ToString();
				Obj.n_form =  row["n_form"].ToString();
				Obj.n_content =  row["n_content"].ToString();
				Obj.n_sort_id = (( row["n_sort_id"])==DBNull.Value)?0:Convert.ToInt32( row["n_sort_id"]);
				Obj.n_click = (( row["n_click"])==DBNull.Value)?0:Convert.ToInt32( row["n_click"]);
				Obj.n_is_lock = (( row["n_is_lock"])==DBNull.Value)?0:Convert.ToInt32( row["n_is_lock"]);
				Obj.add_time = (( row["add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( row["add_time"]);
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  news 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>news 数据实体</returns>
		public NewsEntity Populate_NewsEntity_FromDr(IDataReader dr)
		{
			NewsEntity Obj = new NewsEntity();
			
				Obj.n_id = (( dr["n_id"])==DBNull.Value)?0:Convert.ToInt32( dr["n_id"]);
				Obj.category_id = (( dr["category_id"])==DBNull.Value)?0:Convert.ToInt32( dr["category_id"]);
				Obj.user_id = (( dr["user_id"])==DBNull.Value)?0:Convert.ToInt32( dr["user_id"]);
				Obj.n_title =  dr["n_title"].ToString();
				Obj.n_author =  dr["n_author"].ToString();
				Obj.n_form =  dr["n_form"].ToString();
				Obj.n_content =  dr["n_content"].ToString();
				Obj.n_sort_id = (( dr["n_sort_id"])==DBNull.Value)?0:Convert.ToInt32( dr["n_sort_id"]);
				Obj.n_click = (( dr["n_click"])==DBNull.Value)?0:Convert.ToInt32( dr["n_click"]);
				Obj.n_is_lock = (( dr["n_is_lock"])==DBNull.Value)?0:Convert.ToInt32( dr["n_is_lock"]);
				Obj.add_time = (( dr["add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( dr["add_time"]);
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个News对象
		/// </summary>
		/// <param name="n_id">n_id</param>
		/// <returns>News对象</returns>
		public NewsEntity Get_NewsEntity (int n_id)
		{
			NewsEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@n_id",SqlDbType.Int)
			};
			_param[0].Value=n_id;
			string sqlStr="select * from News where n_id=@n_id";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_NewsEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表News所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< NewsEntity> Get_NewsAll()
		{
			IList< NewsEntity> Obj=new List< NewsEntity>();
			string sqlStr="select * from News";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_NewsEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="n_id">n_id</param>
        /// <returns>是/否</returns>
		public bool IsExistNews(int n_id)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@n_id",SqlDbType.Int)
                                  };
            _param[0].Value=n_id;
            string sqlStr="select Count(*) from News where n_id=@n_id";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
